#include "worker.h"

#include <QFile>

Worker::Worker(QObject *parent) : QObject(parent)
{

}

void Worker::copyFile(const QString &srcFile, const QString &destFile)
{
    QFile f1(srcFile), f2(destFile);

    f1.open(QIODevice::ReadOnly);
    f2.open(QIODevice::WriteOnly | QIODevice::Truncate);

    char buff[4 * 1024];
    qint64 ret, copyCnt = 0, fileSize = f1.size(), preProgress = 0, curProgress = 0;

    while((ret = f1.read(buff, sizeof (buff))) > 0)
    {
        f2.write(buff, ret);

        copyCnt += ret;

        curProgress = 100.0 * copyCnt / fileSize;

        if(curProgress > preProgress)
        {
            emit updateCopyProgress(curProgress);

            preProgress = curProgress;
        }
    }

    emit copyFinished();

}



